package com.kylin.core.commodity.query.handler;

import com.kylin.commodity.api.dto.resp.CommentRespDTO;
import com.kylin.commodity.api.dto.resp.CommodityLiteRespDTO;
import com.kylin.commodity.api.dto.resp.CommodityRespDTO;
import com.kylin.dal.entity.commodity.CommentEntry;
import com.kylin.dal.entity.commodity.CommodityAlbumEntry;
import com.kylin.dal.entity.commodity.CommodityEntry;
import com.kylin.domain.commodity.vo.AlbumVO;
import com.kylin.domain.commodity.vo.CommentVO;
import com.kylin.domain.commodity.vo.CommodityVO;
import com.kylin.domain.marketing.vo.MarketingCommodityVO;
import com.kylin.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 商品对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
public interface CommodityTranslator {

    CommodityVO toCommodityVO(CommodityEntry entry);

    CommentVO toCommentVO(CommentEntry entry);

    @Mapping(source = "createdAt", target = "commentedAt")
    CommentRespDTO toCommentRespDTO(CommentVO vo);

    AlbumVO toAlbumVO(CommodityAlbumEntry entry);

    CommodityLiteRespDTO toCommodityLiteRespDTO(CommodityVO vo);

    CommodityRespDTO toCommodityRespDTO(CommodityVO vo);

    default List<CommodityVO> toCommodityVO(List<CommodityEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream()
                .map(this::toCommodityVO)
                .collect(Collectors.toList());
    }

    default List<AlbumVO> toAlbumVO(List<CommodityAlbumEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream()
                .map(this::toAlbumVO)
                .collect(Collectors.toList());
    }

    default List<CommodityRespDTO> toCommodityRespDTO(List<CommodityVO> commodities) {
        return Optional.ofNullable(commodities)
                .orElse(Collections.emptyList())
                .stream()
                .map(this::toCommodityRespDTO)
                .collect(Collectors.toList());
    }

    default List<CommodityLiteRespDTO> toCommodityLiteRespDTO(List<CommodityVO> commodities) {
        return Optional.ofNullable(commodities)
                .orElse(Collections.emptyList())
                .stream()
                .map(this::toCommodityLiteRespDTO)
                .collect(Collectors.toList());
    }

    default List<CommentVO> toCommentVO(List<CommentEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream()
                .map(this::toCommentVO)
                .collect(Collectors.toList());
    }

    default List<CommentRespDTO> toCommentRespDTO(List<CommentVO> vos) {
        return Optional.ofNullable(vos)
                .orElse(Collections.emptyList())
                .stream()
                .map(this::toCommentRespDTO)
                .collect(Collectors.toList());
    }

    default void loadMarketingCommodityVO(CommodityRespDTO commodityResp, MarketingCommodityVO mCommodityVO) {
        commodityResp.setPrice(mCommodityVO.getPrice());
        commodityResp.setInventory(mCommodityVO.getInventory());
    }
}
